package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.common.result.Result;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;
@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    private MinioConstantProperties minioConstantProperties;

    @Override
    public String upload(MultipartFile file) {
        // 返回数据的时候，返回值：文件上传之后的地址url
        String url = "";
        // 调用 minio 的上传API！
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(minioConstantProperties.getEndpointUrl())
                            .credentials(minioConstantProperties.getAccessKey(), minioConstantProperties.getSecreKey())
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioConstantProperties.getBucketName()).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("asiatrip").build());
            } else {
                System.out.println("Bucket "+ minioConstantProperties.getBucketName()+" already exists.");
            }

            //  String name = atguigu.jpg indexOf lastIndoxOf substring equals compareTo trim replaceAll split
            String myFileName = UUID.randomUUID().toString().replaceAll("-","")
                    +file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            System.out.println("myFileName = " + myFileName);
            // Upload known sized input stream.
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(minioConstantProperties.getBucketName())
                            .object(myFileName)
                            .stream(file.getInputStream(),file.getSize(),-1)
                            .contentType(file.getContentType())
                            .build());
            //返回数据url 没有赋值；url/bucketName/fileName
            //  http://192.168.200.130:9000
            url = minioConstantProperties.getEndpointUrl() +"/" +
                    minioConstantProperties.getBucketName() +"/" + myFileName;
            System.out.println("url = " + url);
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
        return url;
    }
}
